package com.civil.controller;

import java.util.HashMap;
import java.util.Map;

import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.civil.response.DataResponse;
import com.civil.response.ImgUtils;
import com.civil.response.PicUploadResult;
import com.civil.service.UserInfoService;



@RestController
@RequestMapping("/admin/user/")
public class UserInfoController {

	@Value("${file.url.Qrcode}")
	private String urlQrcode;

	@Value("${file.url.path}")
	private String imgUrl;
	
	@Autowired
	UserInfoService userInfoService;
	
	@RequestMapping("insert")
	public DataResponse insert(@RequestBody Map<String, Object> params) {
		
		userInfoService.insert(params);
		return DataResponse.isOk();
	}
	
	@RequestMapping("findList")
	public DataResponse findList(@RequestBody Map<String, Object> params){
		
		Map<String, Object> result = userInfoService.findList(params);
		return DataResponse.isOk(result.get("list"),Integer.valueOf(result.get("count").toString()));
	}
	
	
	@RequestMapping("findDetailById")
	public DataResponse findDetailById(String id){
		
		Map<String, Object> result = userInfoService.findDetailById(id);
		return DataResponse.isOk(result);
	}
	
	
	@RequestMapping("updateById")
	public DataResponse updateById(@RequestBody Map<String, Object> params){
		
		userInfoService.updateById(params);
		return DataResponse.isOk();
	}
	
	@RequestMapping("deleteById")
	public DataResponse deleteById(String id){
		
		userInfoService.deleteById(id);
		return DataResponse.isOk();
	}
	
	
	@RequestMapping("login")
	public DataResponse login(@RequestBody Map<String, Object> params){
		
		
		Map<String, Object> result = userInfoService.login(params);
		if(result == null){
			return DataResponse.ErrorMsg();
		}
		return DataResponse.isOk(result);
	}
	
	
	@RequestMapping(value="uploadImg")
	public DataResponse uploadImg(@PathParam("file") MultipartFile file) {

		PicUploadResult resutl = ImgUtils.tpsupload(file, imgUrl, urlQrcode);
		if (resutl.getCode() == 0) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("picUrl", resutl.getUrl());
			map.put("imgUrl", urlQrcode + resutl.getUrl());
			return DataResponse.isOk(map);
		}
		return DataResponse.ErrorMsg("上传失败");
	}
	
}
